<template>
  <!-- 导航部分 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>质量管理</el-breadcrumb-item>
        <el-breadcrumb-item>常见缺陷</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!--搜索栏-->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form inline>
        <el-form-item label="设备编码">
          <el-input v-model="ledgerQuery.machineCode"/>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="ledgerQuery.machineName"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadLedger" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin: 20px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd">
        <el-icon>
          <Plus/>
        </el-icon>
        新增
      </el-button>
      <el-button type="success" plain @click="toUpdate">
        <el-icon>
          <EditPen/>
        </el-icon>
        编辑
      </el-button>
      <el-button type="warning" plain @click="toDeletes">
        <el-icon>
          <EditPen/>
        </el-icon>
        删除
      </el-button>
      <el-button type="danger" plain @click="toPush">
        <el-icon>
          <Delete/>
        </el-icon>
        导出
      </el-button>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <!--  <el-row style="margin: 10px">-->
  <!--    <el-col :span="24">-->
  <!--      <el-button type="primary" @click="toAdd">新增</el-button>-->
  <!--      <el-button type="primary" @click="toUpdate">编辑</el-button>-->
  <!--      <el-button type="primary" @click="toDeletes">删除</el-button>-->
  <!--      <el-button type="primary" @click="toPush">导出</el-button>-->
  <!--    </el-col>-->
  <!--  </el-row>-->

  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="ledgers" border stripe ref="leaderTable">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="machineCode" label="设备编码">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.machineCode)" type="success">
              {{ scope.row.machineCode }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="machineName" label="设备名称"/>
        <el-table-column prop="machineBrand" label="设备品牌"/>
        <el-table-column prop="machineSpec" label="规格型号"/>
        <el-table-column prop="workshop.workShopName" label="所属车间"/>
        <el-table-column prop="machineStatus" label="设备状态">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.machineStatus == 1">启用</el-tag>
            <el-tag type="warning" v-if="scope.row.machineStatus == 2">停机</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link style="margin: 0px 5px" @click="toEdit(scope.row.machineId)" type="primary">
              <el-icon>
                <EditPen/>
              </el-icon>
              编辑
            </el-link>
            <el-link style="margin: 0px 5px" @click="toDelete(scope.row.machineId)" type="primary">
              <el-icon>
                <Delete/>
              </el-icon>
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>

  <!-- 新增的弹窗 -->
  <el-dialog v-model="addMachineLedgerVisible"
             title="新增台账"
             width="50%"
  >
    <el-form :model="newLedger">
      <el-form-item label="设备编码">
        <el-input v-model="newLedger.machineCode"/>
      </el-form-item>
      <el-form-item label="设备名称">
        <el-input v-model="newLedger.machineName"/>
      </el-form-item>
      <el-form-item label="品牌">
        <el-input v-model="newLedger.machineBrand"/>
      </el-form-item>
      <el-form-item label="规格型号">
        <el-input v-model="newLedger.machineSpec"/>
      </el-form-item>
      <el-form-item label="所属车间">
        <el-select v-model="newLedger.workShopName" placeholder="请选择车间">
          <el-option
              v-for="shop in workShops"
              :key="shop.workShopId"
              :label="shop.workShopName"
              :value="shop.workShopId">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="所属车间">-->
      <!--        <el-input v-model="newLedger.workShopId"/>-->
      <!--      </el-form-item>-->
      <el-form-item label="设备状态">
        <el-select style="width: 100px" v-model="newLedger.machineStatus">
          <el-option :value="1" label="启用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="newLedger.machineNotes"/>
      </el-form-item>

    </el-form>
    <el-button @click="addMachineLedgerVisible =false" newLedger={}>取消</el-button>
    <el-button type="primary" @click="addLedger">
      确定
    </el-button>
  </el-dialog>

  <!--编辑子组件-->
  <LedgerEdit :machineId="updateMachineLedgerId" @editComplate="loadLedger"/>

  <!--详情子组件-->
  <LedgerDetail :machineCode="detailMachineId"/>

</template>

<script setup>

import {onMounted, ref} from "vue";
import ledgerRequest from "@/api/ledger.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {Delete, EditPen} from "@element-plus/icons-vue";
import LedgerEdit from "@/views/machineType/machineLedgerEdit.vue";
import LedgerDetail from "@/views/machineType/machineLedgerDetail.vue";
import login from "@/api/login.js";

let ledgerQuery = ref({});
let ledgers = ref([]);
let curPage = ref(1);
let total = ref(0);
let addMachineLedgerVisible = ref(false);
let newLedger = ref({});
let leaderTable = ref([]);
let updateMachineLedgerId = ref(-1);
let workShops = ref({});
function loadWorkShop(){
  ledgerRequest.selectByWorkShop(res=>{
    workShops.value=res.data;
  })
  console.log(workShops);
}
//导出
function toPush() {
  ledgerRequest.exportLedger(ledgerQuery.value, curPage.value, 10)
}

//显示详情
let detailMachineId = ref(-1);

function showDetail(machineCode) {
  detailMachineId.value = machineCode;
}

//工具栏添加
function toAdd() {
  loadWorkShop();
  addMachineLedgerVisible.value = true;
}

//工具栏添加提交按钮
function addLedger() {
  ledgerRequest.addLedger(newLedger.value, res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "添加成功"
      })
    } else {
      ElMessage({
        type: "warning",
        message: "添加失败"
      })
    }
    newLedger.value = {};
    addMachineLedgerVisible.value = false;
    loadLedger();
  })
}

//工具栏编辑
function toUpdate() {
  let selectionRows = leaderTable.value.getSelectionRows();
  if (selectionRows.length > 1) {
    ElMessage({
      type: "warning",
      message: "只能选择一条数据"
    })
  } else if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
  } else {
    toEdit(selectionRows[0].machineId);
  }
}

//工具栏删除
function toDeletes() {
  let selectionRows = leaderTable.value.getSelectionRows();
  if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    selectionRows.forEach(row => {
      ledgerRequest.deleteLedger(row.machineId, res => {
        console.log(row.machineId);
        if (res.code == 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          })
        } else {
          ElMessage({
            type: "warning",
            message: "删除失败",
          })
        }
      })
    })
    loadLedger();
  })
}

//行编辑
function toEdit(machineId) {
  updateMachineLedgerId.value = machineId;
}

//行删除
function toDelete(machineId) {
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    ledgerRequest.deleteLedger(machineId, res => {
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "操作完成"

        })
        loadLedger();
      } else {
        ElMessage({
          type: "warning",
          message: "删除失败"
        })
      }
    })
    loadLedger();
  })
}

//加载设备
function loadLedger() {
  ledgerRequest.loadLedger(ledgerQuery.value, curPage.value, 10, res => {
    total.value = res.data.total;
    ledgers.value = res.data.list;
    console.log(ledgers);
  })
}

//分页
function changePage(value) {
  curPage.value = value;
  loadLedger();
}

onMounted(() => {
  loadLedger();
})
</script>

<style scoped>

</style>